package org.boulderfoodrescue.webapp.client.managers;

import org.boulderfoodrescue.webapp.client.ApiKeys;
import org.boulderfoodrescue.webapp.client.events.MapsUpdatedEvent;
import org.boulderfoodrescue.webapp.client.events.MapsUpdatedHandler;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.maps.client.Maps;

public class MapsApiManager {
	private final EventBus eventBus;
	public MapsApiManager(EventBus eventBus)
	{
		this.eventBus = eventBus;
		bind();
	}
	
	private void bind()
	{
		eventBus.addHandler(MapsUpdatedEvent.getType(),
				new MapsUpdatedHandler(){
					@Override
          public void onMapsApiLoadSuccess(MapsUpdatedEvent event) {
          }
					@Override
          public void onMapsApiLoadStart(MapsUpdatedEvent event) {
	          loadGoogleMapsApi();
          }
					@Override
          public void onMapsApiLoadFailed(MapsUpdatedEvent event) {
          }
			
		});
	}
	
	private void loadGoogleMapsApi()
	{
		Maps.loadMapsApi(ApiKeys.getMAPS_API_KEY(), "2", false, new Runnable(){
			@Override
      public void run() {
	      eventBus.fireEvent(new MapsUpdatedEvent(MapsUpdatedEvent.ApiState.Loaded));
      }
		});
	}
}
